<?php
namespace plugins\log\lib;
use think\Request;
use plugins\log\model\SysLogsModel;
use plugins\log\model\SysLogsConfigModel;
use cmf\lib\interfaceauth\InterfaceAuth;

//操作监听
class ListenOperate{
    
    private  $request = null;
    private  $slm;
    
    public function __construct()
    {
        $this->request = Request::instance();
        $this->slm = new SysLogsModel;
    }
    
    //监听开始方法
    public function start( $params )
    {
        
        $level = $this->getLogLevel();
        
        if( $level == 'OFF' ){
            return true;
        }
        
        $slm = $this->slm;
        
        $slm->rule_name = $this->getRuleName(); 
        
        $slm->params = $this->request->param();
        
        list( $uid , $userType ) = $this->getUserInfo();
        
        $slm->uid = $uid;
        
        $slm->user_type = $userType;
        
        $slm->client_ip = get_client_ip( 0 , true );
        
        $slm->rk = is_array( $params ) ? json_encode( $params , JSON_UNESCAPED_UNICODE ) : $params ;
        
        $isWrite = ( $level=='H' ) ? true : false;
        
        if( is_array ( $params ) ){

            $slm->status = $params [ 'code' ];
            $msg = isset( $params [ 'msg' ] ) ? $params [ 'msg' ] : $params [ 'message' ];
            $slm->event = "默认操作:".$msg;
        }else{
            
            $slm->status = "200";
            $slm->event = $params .": 页面访问";
            $msg = $params .": 页面访问";
        }
        
        
        //如果在已有配置里面  
            
        $findConfig = SysLogsConfigModel::where('rule',$slm->rule_name)->find();
        if( !empty( $findConfig) ){
            $alias = !empty( $findConfig->action_alias ) ? $findConfig->action_alias : "默认操作" ;
            $msg = $alias .":".$msg ;
            $slm->event = $msg;
            if( $level == 'M' ){
                
                $isWrite  =  true;
            }
        }
        if( $isWrite ){
            $slm->save();
        }
    }
    
    
    //获取请求规则
    private function getRuleName( )
    {
        $ruleName = '';
        $param = $this->request->param();
        //判断是否是模块请求
        if( !empty( $this->request->module() ) ){
            //非模块请求
            $app = $this->request->module();
            $controller = $this->request->controller();
            $action = $this->request->action();
            $ruleName = strtolower( "$app/$controller/$action" );
            
        }else if( isset( $param [ '_plugin' ] ) && !empty( $param [ '_plugin' ] )  ){
            //模块请求
            $app = $param [ '_plugin' ];
            $controller = $param [ '_controller' ];
            $action = $param [ '_action' ];
            $ruleName = strtolower( "plugin/$app/$controller/$action" );
        }
        
        return $ruleName;
    }
    
    
    //获取用户信息
    private function getUserInfo()
    {
    
        if( isset( $this->request->param() ['proof'] ) ){
            //解析token
            $uid = $this->getApiUid();
            $uid = !empty( $uid ) ? $uid : "0" ;
            $userType = 1;
            if( $uid == "0" ){
                $uid = $this->getApiUid( true );
                $uid = !empty( $uid ) ? $uid : "0" ;
                if( $uid != "0" ){
                    $userType = 2;
                }
            }
        }else{
            $uid = cmf_get_current_admin_id();
            $userType = 2;
        }
        
        if( $uid == "0" ){
            $userType = 0;
        }
        
        return [ $uid , $userType ];
    }
    
    
    //获取用户根据token请求来的uid
    private function getApiUid( $isUser = false )
    {
        try {
            $params = $this->request->param();
            $res = InterfaceAuth::verificationToken( $params [ 'proof' ] );
           
            if( is_array( $res ) &&  isset( $res['status'] ) && $res [ 'status' ] == false ){

                return '';
            }
    
            if( !isset( $res -> uid ) || empty( $res ->uid ) ){
                return '';
            }
            try {
                if( $isUser ){
                    $info = Model('\\app\\admin\\model\\UserModel')->where('id',$res->uid )->find();
                }else{
                    $info = Model('\\plugins\\member\\model\\MemberModel')->where('uid',$res->uid )->find();
                }
                
            } catch ( \Exception $e ) {
                return '';
            }
            if( empty( $info ) ){
                return '';
            }
            return $res->uid;
        } catch (\Exception  $e ) {
            return '';
        }
    }
    
    
    private function getLogLevel()
    {
        try {
            $level = file_get_contents( PLUGINS_PATH.'log'.DS.'config'.DS.'20200813.txt' );
        } catch ( \Exception $e ) {
            $level = 'M';
        }
        
        $arr = [ "H" , "M" , "OFF" ];
        
        return in_array( $level , $arr ) ? $level : 'M' ;
    }
    
    
}